package org.biubiu0002.baiblog.common.utils;

import org.biubiu0002.baiblog.common.exception.BizCodeEnumException;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: biubiu0002
 * @Date: 2022-05-06 0:57
 **/
public class R extends HashMap<String,Object> {
    private static final long serialVersionUID = 1L;

    public R() {
//        put("code", 0);
//        put("msg", "success");
    }

    public static R error() {
        return error(BizCodeEnumException.UNKNOWN_SERVER_EXCEPTION.getCode(), "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(BizCodeEnumException.UNKNOWN_SERVER_EXCEPTION.getCode(), msg);
    }

    public static R error(BizCodeEnumException exception) {
        R r = new R();
        r.put("errorCode",exception.getCode());
        r.put("errorMsg",exception.getMsg());
        return r;
    }

    public static R error(BizCodeEnumException exception,String msg) {
        R r = new R();
        r.put("errorCode",exception.getCode());
        r.put("errorMsg",msg);
        return r;
    }

    public static R error(String code, String msg) {
        R r = new R();
        r.put("errorCode", code);
        r.put("errorMsg", msg);
        return r;
    }

    public static R error(Throwable throwable){
        R r = new R();
        r.put("errorCode",BizCodeEnumException.UNKNOWN_SERVER_EXCEPTION);
        r.put("errorMsg",throwable.getMessage());
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("code",0);
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.put("code",0);
        r.putAll(map);
        return r;
    }

    public static R ok() {
        R r = new R();
        r.put("code",0);
        r.put("msg","success");
        return r;
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
